//
//  WBStatusViewModel.swift
//  Swift-WB
//
//  Created by lxy on 2017/6/13.
//  Copyright © 2017年 lixiya. All rights reserved.
//

import UIKit

let kWBStatusCellMargin: CGFloat = 15 // 边距
let KWBStatusCellPadding: CGFloat = 10 // 间距
let KWBStatusCellContentLabWidth: CGFloat = kScreenWidth - kWBStatusCellMargin*2 - 40 - KWBStatusCellPadding
let kWBStatusCellPictureWH: CGFloat = CGFloat((KWBStatusCellContentLabWidth - 2*KWBStatusCellPadding)/3)


class WBStatusViewModel: NSObject {
    
    var cellHeight: CGFloat = 0 //cell高度
    var contentLabHeight: CGFloat = 0 //内容文本高度
    var picturesViewHeight: CGFloat = 0 //微博配图高度

    var pic_urls: [WBStatusPictures] = Array() //微博配图数组
    
    var status: WBStatus? {
        didSet { setStatus() }
    }
  
    
}

//MARK: - model处理
extension WBStatusViewModel {
    
    func setStatus() {
        guard let status = status else { return }

        // 初始化数据
        cellHeight = 0
        contentLabHeight = 0
        picturesViewHeight = 0
        
        // 计算内容文本高度
        if let text = status.text {
            contentLabHeight = text.xy_height(width:KWBStatusCellContentLabWidth , font: UIFont.systemFont(ofSize: 14))
        }
        
        // 计算微博配图 ,,单图模式下默认宽高100
        if let pic_urls = status.pic_urls ?? status.retweeted_status?.pic_urls {
            self.pic_urls = pic_urls
            if pic_urls.count == 0 {
                picturesViewHeight = 0
            }else if pic_urls.count == 1{
                picturesViewHeight = 100
            }else if pic_urls.count == 4{
                picturesViewHeight = kWBStatusCellPictureWH*2 + KWBStatusCellPadding
            }else {
                picturesViewHeight = KWBStatusCellPadding*CGFloat(((pic_urls.count-1)/3)) + CGFloat(((pic_urls.count-1)/3+1))*kWBStatusCellPictureWH
            }
        }
        
        
        
        cellHeight = kWBStatusCellMargin + 40 + KWBStatusCellPadding + contentLabHeight + KWBStatusCellPadding + picturesViewHeight + KWBStatusCellPadding

    }

}



